<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>medic</title>
    <link rel="stylesheet" href="/Trees/LayUiBac/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-container">
    <div class="layui-row" style="border: cadetblue 1px solid;padding: 20px;margin-top: 30px">
        <h1>用户一览</h1>
        <table id="demo" lay-filter="userTable"></table>
        <div class="layui-col-md4" style="border: cadetblue 1px solid;padding: 20px">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户类别</label>
                    <div class="layui-input-block">
                        <select name="userType" lay-verify="required" id="userType">
                            <option value="10000">全部用户</option>
                            <option value="0">最高等级用户</option>
                            <option value="1">资料等级用户</option>
                            <option value="2">仓库等级用户</option>
                        </select>
                    </div>
                </div>
            </form>
            <div class="layui-col-md12">
                <button class="layui-btn" type="button" id="Find">查询</button>
                <button class="layui-btn layui-bg-orange" type="button" id="buttonAdd">添加新的用户信息</button>
            </div>
        </div>
    </div>
</div>


<script src="/Trees/LayUiBac/layui/layui.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看用户详情</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="delete">删除此用户</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="alt">修改此用户信息</a>
</script>
<script>
    layui.use('table', function () {
        let $ = layui.$;
        let layer = layui.layer;
        let table = layui.table;
        //第一个实例
        let show = table.render({
            elem: '#demo'
            , url: '/Trees/user' //数据接口
            , page: true //开启分页
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "count": res.total, //解析数据长度
                    "data": res.list
                };
            }
            , cols: [[ //表头
                {field: 'pkId', title: 'ID', sort: true, fixed: 'left'}
                , {field: 'user', title: '用户名'}
                , {field: 'level', title: '用户级别'}
                , {field: 'realname', title: '真实姓名'}
                , {fixed: 'right', align: 'center', width: '500', toolbar: '#barDemo'}
            ]]
        });

        $('#Find').on('click', function () {
            show.reload({
                where: {
                    userType: $('#userType').val()
                }
                , page: {
                    curr: 1
                }
            })
        })

        $('#buttonAdd').on('click', function () {
            let a, b, c, d, e, f;
            layer.open({
                type: 2
                , content: "/Trees/LayUiBac/home/user/Jump/addUser.html"//填一个url地址
                , area: ['600px', '600px']
                , shade: ['0.9', '#393D49']
                , anim: 0
                , maxmin: true
                , btn: ["确认提交"]
                , yes: function (index, layero) {
                    let body = layer.getChildFrame('body', index);
                    a = body.find("input[name=userName]").val();
                    b = body.find("input[name=password]").val();
                    c = body.find("select[name=level]").val();
                    d = body.find("input[name=realname]").val();
                    let strs = "&userName=" + a + "&password=" + b + "&level=" + c + "&realname=" + d
                    $.ajax({
                        type: "post",
                        url: "/Trees/user?method=add" + strs

                    })

                    layer.close(index);
                    show.reload();
                }
                , success: function (layero, index) {

                }

            })
        })


        table.on('tool(userTable)', function (obj) {
            let data = obj.data;
            console.log(data)
            let layEvent = obj.event;
            let tr = obj.tr;

            if (layEvent === "alt") {
                layer.open({
                    type: 2
                    , content: "/Trees/LayUiBac/home/user/Jump/addUser.html"//填一个url地址
                    , area: ['600px', '600px']
                    , shade: ['0.9', '#393D49']
                    , anim: 0
                    , maxmin: true
                    , btn: ["确认修改"]
                    , yes: function (index, layero) {
                        let body = layer.getChildFrame('body', index);
                        let b = body.find("input[name=password]").val();
                        let c = body.find("select[name=level]").val();
                        let strs = "&password=" + b + "&level=" + c + "&pkId=" + data.pkId
                        $.ajax({
                            type: "post",
                            url: "/Trees/user?method=alt" + strs
                        })
                        layer.close(index);
                        show.reload();
                    }
                    , success: function (layero, index) {
                        let body = layer.getChildFrame('body', index);
                        body.find("input[name=userName]").val(data.user);
                        body.find("input[name=password]").val(data.password);
                        body.find("select[name=level]").val(data.level);
                        body.find("input[name=realname]").val(data.realname);
                    }
                })
            } else if (layEvent === "delete") {
                $.ajax({
                    type: 'post'
                    , url: '/Trees/user?method=delete' + "&pkId=" + data.pkId
                })
            } else if (layEvent === "detail") {
                layer.open({
                    type: 2
                    , content: "/Trees/LayUiBac/home/user/Jump/showUser.html"//填一个url地址
                    , area: ['600px', '600px']
                    , shade: ['0.9', '#393D49']
                    , anim: 0
                    , maxmin: true
                    , btn: ["返回主菜单"]
                    , yes: function (index, layero) {
                        layer.close(index);
                    }
                    , success: function (layero, index) {
                        let body = layer.getChildFrame('body', index);
                        body.find("input[name=userName]").val(data.user);
                        body.find("input[name=password]").val(data.password);
                        let level;
                        if(data.level===0){
                            level="最高级别用户";
                        }else if(data.level===1){
                            level="资料等级用户";
                        }else if(data.level===2){
                            level ="仓库等级用户"
                        }
                        body.find("input[name=level]").val(level);
                        body.find("input[name=realname]").val(data.realname);
                    }
                })
            }
        });
    })

</script>


</body>
</html>